package com.zhumei.keyscale.weight;

import android.app.*;
import android.content.*;
import android_serialport_api.*;
import android.util.*;
import java.io.*;
import io.reactivex.*;
import io.reactivex.android.schedulers.*;

public class SerialPortCardUtil
{
    public static SerialPortCardUtil serialPortUtil;
    private Activity activity;
    private CardInterface cardInterface;
    public Context context;
    private InputStream inputStream;
    private boolean isStart;
    private ReceiveThread mReceiveThread;
    private OutputStream outputStream;
    private SerialPort serialPort;
    
    public SerialPortCardUtil() {
        this.serialPort = null;
        this.inputStream = null;
        this.outputStream = null;
        this.mReceiveThread = null;
        this.isStart = false;
        this.context = this.context;
        this.getSerialPort();
        this.inputStream = this.serialPort.getInputStream();
        this.outputStream = this.serialPort.getOutputStream();
        this.isStart = true;
        this.startThread();
    }
    
    public SerialPortCardUtil(final String s, final int n) {
        this.serialPort = null;
        this.inputStream = null;
        this.outputStream = null;
        this.mReceiveThread = null;
        this.isStart = false;
        this.context = this.context;
        this.getSerialPort(s, n);
        this.inputStream = this.serialPort.getInputStream();
        this.outputStream = this.serialPort.getOutputStream();
        this.isStart = true;
        this.startThread();
    }
    
    public static SerialPortCardUtil getSerialPortUtil() {
        if (SerialPortCardUtil.serialPortUtil == null) {
            SerialPortCardUtil.serialPortUtil = new SerialPortCardUtil();
        }
        if (SerialPortCardUtil.serialPortUtil.mReceiveThread == null) {
            SerialPortCardUtil.serialPortUtil = new SerialPortCardUtil();
        }
        if (!SerialPortCardUtil.serialPortUtil.mReceiveThread.isAlive()) {
            SerialPortCardUtil.serialPortUtil.mReceiveThread.start();
        }
        return SerialPortCardUtil.serialPortUtil;
    }
    
    public static void init(final Context context) {
        (SerialPortCardUtil.serialPortUtil = new SerialPortCardUtil()).setContext(context);
    }
    
    public static void init(final Context context, final String s, final int n) {
        (SerialPortCardUtil.serialPortUtil = new SerialPortCardUtil(s, n)).setContext(context);
    }
    
    private void startThread() {
        if (this.mReceiveThread == null) {
            this.mReceiveThread = new ReceiveThread();
        }
        this.mReceiveThread.start();
    }
    
    public void analysisData(final String s) {
        final CardInterface cardInterface = this.cardInterface;
        if (cardInterface == null) {
            return;
        }
        cardInterface.number(s);
    }
    
    public void closeSerialPort() {
        Log.i("test", "\u5173\u95ed\u4e32\u53e3");
        try {
            final InputStream inputStream = this.inputStream;
            if (inputStream != null) {
                inputStream.close();
            }
            final OutputStream outputStream = this.outputStream;
            if (outputStream != null) {
                outputStream.close();
            }
            this.isStart = false;
        }
        catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    
    public InputStream getInputStream() {
        if (this.inputStream == null) {
            this.inputStream = this.getSerialPort().getInputStream();
        }
        return this.inputStream;
    }
    
    public SerialPort getSerialPort() {
        if (this.serialPort == null) {
            try {
                this.serialPort = new SerialPort(new File("/dev/ttyS1"), 115200, 0);
            }
            catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        return this.serialPort;
    }
    
    public SerialPort getSerialPort(final String s, final int n) {
        if (this.serialPort == null) {
            try {
                this.serialPort = new SerialPort(new File(s), n, 0);
            }
            catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        return this.serialPort;
    }
    
    public void openSerialPort() {
        try {
            final SerialPort serialPort = new SerialPort(new File("/dev/ttyS1"), 115200, 0);
            this.serialPort = serialPort;
            this.inputStream = serialPort.getInputStream();
            this.outputStream = this.serialPort.getOutputStream();
            this.isStart = true;
        }
        catch (IOException ex) {
            ex.printStackTrace();
        }
        this.startThread();
    }
    
    public void sendSerialPort(final String s) {
        try {
            this.outputStream.write(DataUtils.HexToByteArr(s));
            this.outputStream.flush();
        }
        catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    
    public void setCardInterface(final CardInterface cardInterface) {
        this.cardInterface = cardInterface;
        if (SerialPortCardUtil.serialPortUtil == null) {
            SerialPortCardUtil.serialPortUtil = new SerialPortCardUtil();
        }
    }
    
    public void setContext(final Context context) {
        this.context = context;
    }
    
    public SerialPort setSerialPort(final String s, final int n) {
        try {
            final SerialPort serialPort = new SerialPort(new File(s), n, 0);
            this.serialPort = serialPort;
            this.inputStream = serialPort.getInputStream();
            this.outputStream = this.serialPort.getOutputStream();
            this.isStart = true;
            this.startThread();
        }
        catch (IOException ex) {
            ex.printStackTrace();
        }
        return this.serialPort;
    }
    
    public interface CardInterface
    {
        void number(final String p0);
    }
    
    private class ReceiveThread extends Thread
    {
        @Override
        public void run() {
            super.run();
            while (SerialPortCardUtil.this.isStart) {
                if (SerialPortCardUtil.this.inputStream == null) {
                    return;
                }
                final byte[] array = new byte[1024];
                try {
                    final int read = SerialPortCardUtil.this.inputStream.read(array);
                    if (read <= 0) {
                        continue;
                    }
                    final String byteArrToHex = DataUtils.ByteArrToHex(array, 0, read);
                    if (byteArrToHex == null || byteArrToHex.equals("")) {
                        continue;
                    }
                    Observable.create((ObservableOnSubscribe<Object>)new ObservableOnSubscribe() {
                        @Override
                        public void subscribe(final ObservableEmitter observableEmitter) {
                            SerialPortCardUtil.this.analysisData(byteArrToHex);
                        }
                    }).observeOn(AndroidSchedulers.mainThread()).subscribeOn(AndroidSchedulers.mainThread()).subscribe();
                }
                catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }
    }
}
